<template lang="pug">
  .echartBox#teacherDistEchartsBox(style="height: 420px")
</template>
<script>
import echartMixin from '@/mixins/echarts'
import '../index.scss'

export default {
  mixins: [echartMixin],
  data () {
    return {
      echartsId: 'teacherDistEchartsBox'
    }
  },
  mounted () {
    this.getData()
  },
  methods: {
    getData () {
      const data = [
        { gardenName: '某某幼儿园', teacher: 24, staff: 46 },
        { gardenName: '某某幼儿园', teacher: 54, staff: 86 },
        { gardenName: '某某幼儿园', teacher: 47, staff: 68 },
        { gardenName: '某某幼儿园', teacher: 34, staff: 46 }
      ]
      this.getEcharts(data)
    },
    getEcharts (values) {
      console.log(values)
      const allValue = values.reduce((sum, cur) => {
        return sum + cur.teacher + cur.staff
      }, 0)
      const options = {
        xAxis: {
          type: 'category',
          data: values.map(x => x.gardenName)
        },
        yAxis: {
          type: 'value'
        },
        lineStyle: {
          color: '#5470C6'
        },
        itemStyle: {
          color: '#5470C6'
        },
        tooltip: {
          trigger: 'axis',
          backgroundColor: 'transparent',
          formatter: params => {
            const data = params[0]
            console.log(data)
            return `<div class="echartsTooltop"><h3>${data.name}</h3><p>教保师: ${values[data.dataIndex].teacher}</p><p>职工: ${values[data.dataIndex].staff}</p><p>占比: ${(data.value / allValue).toFixed(2) * 100 + '%'}</p></div>`
          }
        },
        series: [
          {
            data: values.map(x => x.teacher + x.staff),
            type: 'line',
            smooth: true
          }
        ]
      }
      this.$_chartMixin_init(options)
    }
  }
}
</script>
<style lang="scss" scoped>
</style>
